﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Web;
using System.Web.UI;
using Components.Data.Elements;
using Components.Runtime;

namespace Components
{
  public static class Context
  {
    public static Element Element
    {
      get
      {
        if (!ContextDictionary.ContainsKey(ELEMENT_KEY))
          ContextDictionary[ELEMENT_KEY] = ElementRepository.Instance.Get(HttpContext.Current.Request.AppRelativeCurrentExecutionFilePath);

        return ContextDictionary[ELEMENT_KEY] as Element;
      }
      internal set
      {
        ContextDictionary[ELEMENT_KEY] = value;
      }
    }

    private static Dictionary<String, Object> ContextDictionary
    {
      get
      {
        if (HttpContext.Current.Items[CONTEXTDICTIONARY_KEY] == null)
          HttpContext.Current.Items[CONTEXTDICTIONARY_KEY] = new Dictionary<String, Object>();

        return HttpContext.Current.Items[CONTEXTDICTIONARY_KEY] as Dictionary<String, Object>;
      }
    }

    private const String CONTEXTDICTIONARY_KEY = "components_context";
    private const String ELEMENT_KEY = "element";
  }
}